<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
	
	<cffunction name="uploadThumbnail" access="public" output="false" returntype="string">
		<cfargument name="fundID" required="true" />
		<cfargument name="suffix" required="true" />
		<cfargument name="x" required="true" />
		<cfargument name="y" required="true" />
		<cfargument name="w" required="true" />
		<cfargument name="h" required="true" />
		
		<cfset var local = {} />
		
		<!--- check login credentials --->
		<cfset local.fund = application.s.fundService.getIDByFundID(fundID=arguments.fundID) />
		
		<cfif not session.loggedin>
			<cfthrow message="Not logged in. ">
		</cfif>
		
		<cfif session.user.id neq local.fund.user_id>
			<cfthrow message="Not your fund.">
		</cfif>
		
		<cfimage action="read" name="local.image" source="https://s3.amazonaws.com/FundrImages/#arguments.fundID#/full.#arguments.suffix#" />
		
		<cfset imageCrop(
			name=local.image,
			x=arguments.x,y=arguments.y,width=arguments.w,height=arguments.h
		) />
		<cfset imageResize(
			name=local.image,
			width=150,height=95,interpolation='mediumQuality'
		) />
		
		<cfset local.path = expandPath('../') />
		
		<cfimage action="write" destination="#local.path#/temp/#arguments.fundID#.png" source="#local.image#" overwrite="yes" />

		<cfset application.s3.putObject(bucketName='FundrImages',keyName='#arguments.fundiD#/thumb.png',fileKey='#arguments.fundID#.png',contentType='image/png',uploadDir='../temp/') />

		<cffile action="delete" file="#local.path#/temp/#arguments.fundID#.png" />
		
		<cfset $.database.save(
			table="fund",
			id=local.fund.id,
			has_thumbnail=1
		) />
		
		<cfreturn true />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="removeThumbnail" access="public" output="false" returntype="string">
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<!--- check login credentials --->
		<cfset local.fund = application.s.fundService.getIDByFundID(fundID=arguments.fundID) />
		
		<cfif not session.loggedin>
			<cfthrow message="Not logged in. ">
		</cfif>
		
		<cfif session.user.id neq local.fund.user_id>
			<cfthrow message="Not your fund.">
		</cfif>
		
		<cfset $.database.save(
			table="fund",
			id=local.fund.id,
			has_thumbnail=0
		) />
		
		<cfreturn true />
		
	</cffunction>

	<!--- --->

</cfcomponent>